package com.skeeey.aim.service.dao.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.skeeey.aim.model.Workload;
import com.skeeey.aim.service.dao.DaoException;
import com.skeeey.aim.service.dao.WorkloadDao;

public class WorkloadDaoImpl  extends TempleteDaoImpl<Workload, String> implements WorkloadDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<Workload> getWorkloadsBy(String year, String quarter, String userName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Workload.class);
		criteria.add(Restrictions.eq("userName", userName));
		criteria.add(Restrictions.eq("year", year));
		criteria.add(Restrictions.eq("quarter", quarter));
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Workload> getWorkloadsBy(String year, String quarter) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Workload.class);
		criteria.add(Restrictions.eq("year", year));
		criteria.add(Restrictions.eq("quarter", quarter));
		return this.getHibernateTemplate().findByCriteria(criteria);
	}

	@Override
	public Workload findWorkloadBy(String userName, String dealerName, String finishDate) {
		Workload workload = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(Workload.class);
		criteria.add(Restrictions.eq("userName", userName));
		criteria.add(Restrictions.eq("dealerName", dealerName));
		criteria.add(Restrictions.eq("finishDate", finishDate));
		@SuppressWarnings("unchecked")
		List<Workload> list = this.getHibernateTemplate().findByCriteria(criteria);
		if(list.size() > 0) {
			workload = list.get(0);
		}
		return workload;
	}

	@Override
	public Workload findWorkloadBy(String dealerName, String finishDate) {
		Workload workload = null;
		DetachedCriteria criteria = DetachedCriteria.forClass(Workload.class);
		criteria.add(Restrictions.eq("dealerName", dealerName));
		criteria.add(Restrictions.eq("finishDate", finishDate));
		@SuppressWarnings("unchecked")
		List<Workload> list = this.getHibernateTemplate().findByCriteria(criteria);
		if(list.size() > 0) {
			workload = list.get(0);
		}
		return workload;
	}

	@Override
	public void deleteWorkloadBy(String dealerName, String finishDate) {
		Workload workload = findWorkloadBy(dealerName, finishDate);
		if(workload != null) {
			try {
				this.delete(workload);
			} catch (DaoException e) {
				e.printStackTrace();
			}
		}
	}
}
